package com.shen.llmtool.modules.dify.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shen.llmtool.common.constant.CsvConstant;
import com.shen.llmtool.common.constant.StandardNameConstant;
import com.shen.llmtool.common.utils.CsvFileUtil;
import com.shen.llmtool.common.utils.OkHttpUtil;
import com.shen.llmtool.modules.dify.vo.LLMDataVo;
import com.shen.llmtool.modules.dify.vo.LLMInputVo;
import com.shen.llmtool.modules.dify.vo.LLMOutputVo;
import lombok.extern.slf4j.Slf4j;

import java.util.*;


/**
 * 机构名称标准化
 */
@Slf4j
public class StandardNameService {
    public static void main(String[] args) {
//        oneCallLLM();
        repeatCallLLM();
    }

    /**
     * 单次调用LLM
     */
    public static void oneCallLLM() {
        String csvReadFilePath = "E:\\origin.csv";
        String csvWriteFilePath = "E:\\standard.csv";
        List<String> jgmcList = CsvFileUtil.readCsvFile(csvReadFilePath);
        StringJoiner stringJoiner = new StringJoiner("\n");
        for (String originName : jgmcList) {
            stringJoiner.add(originName);
        }
        String inputStr = stringJoiner.toString();
        Map<String, Object> inputMap = new HashMap<>(1 << 1);
        inputMap.put(StandardNameConstant.INPUT_MAP_KEY, inputStr);
        LLMInputVo llmInputVo = new LLMInputVo(inputMap, StandardNameConstant.BLOCKING_MODE, StandardNameConstant.WORKFLOW_USER);
        Map<String, String> headMap = new HashMap<>(1 << 1);
        headMap.put(StandardNameConstant.AUTHORIZATION_KEY, StandardNameConstant.AUTHORIZATION_VALUE);
        String responseData = OkHttpUtil.post(headMap, StandardNameConstant.LLM_STANDARD_NAME_URL, JSON.toJSONString(llmInputVo));
        LLMOutputVo llmOutputVo = JSONObject.parseObject(responseData, LLMOutputVo.class);
        LLMDataVo llmDataVo = llmOutputVo.getData();
        JSONObject jsonObject = (JSONObject) llmDataVo.getOutputs();
        String standardNameStr = jsonObject.getString("text");
        String[] standardNameArray = JSONArray.parseArray(standardNameStr).toArray(new String[0]);
        List<String[]> standardNameList = new ArrayList<>();
        for (String standardName : standardNameArray) {
            String[] record = new String[1];
            record[0] = standardName;
            standardNameList.add(record);
            System.out.println(standardName);
        }
        CsvFileUtil.writeCsvFile(csvWriteFilePath, standardNameList, CsvConstant.STANDARD_CSV_HEADER);
    }

    /**
     * 多次调用LLM
     */
    public static void repeatCallLLM() {
        String csvReadFilePath = "E:\\origin.csv";
        String csvWriteFilePath = "E:\\standard.csv";
        List<String> jgmcList = CsvFileUtil.readCsvFile(csvReadFilePath);
        List<String[]> standardNameList = new ArrayList<>();
        //临时变量
        StringJoiner stringJoiner = new StringJoiner("\n");
        int batchSize = 0;
        for (int i = 0; i < jgmcList.size(); i++) {
            batchSize++;
            stringJoiner.add(jgmcList.get(i));
            if (batchSize >= 100) {
                String inputStr = stringJoiner.toString();
                requestLLM(inputStr, standardNameList);
                batchSize = 0;
                stringJoiner = new StringJoiner("\n");
            }
            stringJoiner.add(jgmcList.get(i));
        }
        for (String originName : jgmcList) {
            stringJoiner.add(originName);
        }
        CsvFileUtil.writeCsvFile(csvWriteFilePath, standardNameList, CsvConstant.STANDARD_CSV_HEADER);
    }

    /**
     * 请求调用LLM
     *
     * @param inputStr         输入字符串
     * @param standardNameList 标准化名称列表
     */
    public static void requestLLM(String inputStr, List<String[]> standardNameList) {
        Map<String, Object> inputMap = new HashMap<>(1 << 1);
        inputMap.put(StandardNameConstant.INPUT_MAP_KEY, inputStr);
        LLMInputVo llmInputVo = new LLMInputVo(inputMap, StandardNameConstant.BLOCKING_MODE, StandardNameConstant.WORKFLOW_USER);
        Map<String, String> headMap = new HashMap<>(1 << 1);
        headMap.put(StandardNameConstant.AUTHORIZATION_KEY, StandardNameConstant.AUTHORIZATION_VALUE);
        String responseData = OkHttpUtil.post(headMap, StandardNameConstant.LLM_STANDARD_NAME_URL, JSON.toJSONString(llmInputVo));
        LLMOutputVo llmOutputVo = JSONObject.parseObject(responseData, LLMOutputVo.class);
        LLMDataVo llmDataVo = llmOutputVo.getData();
        JSONObject jsonObject = (JSONObject) llmDataVo.getOutputs();
        String standardNameStr = jsonObject.getString("text");
        String[] standardNameArray = JSONArray.parseArray(standardNameStr).toArray(new String[0]);
        for (String standardName : standardNameArray) {
            String[] record = new String[1];
            record[0] = standardName;
            standardNameList.add(record);
        }
    }
}
